from rest_framework_jwt.settings import api_settings

jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER


def jwt_response_payload_handler(token, user=None, request=None):

    # token 就是 JWT生成的token
    # user 就是登陆之后的user 是User的实例对象
    return {
        'token': token,
        'username': user.username,
        'user_id':user.id
    }



def set_token(user):

    payload = jwt_payload_handler(user)
    token = jwt_encode_handler(payload)
    user.token = token

    return token
